﻿using CinemaMaestro.BusinessLayer.Contracts;
using CinemaMaestro.DataLayer.Contracts;
using CinemaMaestro.DataLayer.Contracts.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CinemaMaestro.BusinessLayer
{
    public class Movies : BusinessObjectBase, IMovies
    {
        public Movies(IUnitOfWork uow)
            : base(uow)
        { }

        public Movie Create(Movie movie)
        {
            var createdMovie = _uow.MovieRepository.Create(movie);
            _uow.SaveChanges();
            return createdMovie;
        }

        public bool Delete(int id)
        {
            var deleted = _uow.MovieRepository.Delete(id);
            _uow.SaveChanges();
            return deleted;
        }

        public IQueryable<Movie> Read()
        {
            return _uow.MovieRepository.Read();
        }

        public void Update(Movie entity)
        {
            _uow.MovieRepository.Update(entity);
            _uow.SaveChanges();
        }

        public bool MovieHasRelationWithCategory(int categoryId)
        {
            bool hasRelation = Read().Any(x => x.Categories.Any(c => c.Id == categoryId));
            return hasRelation;
        }

        public bool MovieHasRelationWithDirector(int directorId)
        {
            bool hasRelation = Read().Any(x => x.Directors.Any(c => c.Id == directorId));
            return hasRelation;
        }

        public bool MovieHasRelationWithActor(int actorId)
        {
            bool hasRelation = Read().Any(x => x.Actors.Any(c => c.Id == actorId));
            return hasRelation;
        }

    }
}
